package com.text.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class FileCtrl {
    @ResponseBody
    @RequestMapping("/uploadFile")
    public String uploadFile(MultipartFile file){
        String fileName= file.getOriginalFilename();
        System.out.println(fileName);
        fileName = UUID.randomUUID()+"_"+fileName;
        System.out.println(fileName);

        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
            File uploadpath = new File(path.getAbsolutePath(),"static/local/image/goods/");

            File f = new File(uploadpath,fileName);

            //file.transferTo(new File(uploadpath,fileName));
            file.transferTo(f);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
